/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name:login.c
> Author:xxx
> Mail: 
> Created Time: 2024年12月04日 星期三 11时59分19秒
***************************************************/

#include "cJSON.h"
#include "login.h"

//管理员、员工、家属登陆
int login(int client_socket,int num){
	char username[20] = {0};
	char passwd[20] = {0};
	printf("input gly账号:");
	scanf("%s",username);
	printf("input gly密码:");
	scanf("%s",passwd);
	
	//创建json对象
	cJSON* root = cJSON_CreateObject();
	cJSON* type;
	if(num == 1){
		type = cJSON_CreateString("gly_login");//管理员登陆
	}else if(num == 2){
		type = cJSON_CreateString("staff_login");//员工登陆
	}else{
		type = cJSON_CreateString("kin_login");//家属登陆
	}
	cJSON* username_0 = cJSON_CreateString(username);
	cJSON* passwd_0 = cJSON_CreateString(passwd);
	
	cJSON_AddItemToObject(root,"type",type);
	cJSON_AddItemToObject(root,"username",username_0);
	cJSON_AddItemToObject(root,"passwd",passwd_0);
	
	char str[1024] = {0};
	strcpy(str,cJSON_Print(root));
	printf("%s\n",str);
	write(client_socket,str,strlen(str));
	memset(str,0,1024);
	read(client_socket,str,sizeof(str));
	printf("%s\n",str);
	if(strcmp(str,"success") == 0){
		return 1;
	}
	return 0;
}




//员工、家属注册
int register_0(int client_socket,int num){
	char username[20] = {0};
	char passwd[20] = {0};
	printf("input 用户名:");
	scanf("%s",username);
	printf("input 密码:");
	scanf("%s",passwd);
	
	//创建json对象
	cJSON* root = cJSON_CreateObject();
	cJSON* type;
	if(num == 1){
		type = cJSON_CreateString("staff_register");//员工注册
	}else{
		type = cJSON_CreateString("kin_register");//家属注册
	}
	cJSON* username_0 = cJSON_CreateString(username);
	cJSON* passwd_0 = cJSON_CreateString(passwd);
	
	cJSON_AddItemToObject(root,"type",type);
	cJSON_AddItemToObject(root,"username",username_0);
	cJSON_AddItemToObject(root,"passwd",passwd_0);
	
	char str[1024] = {0};
	strcpy(str,cJSON_Print(root));
	printf("%s\n",str);
	write(client_socket,str,strlen(str));
	memset(str,0,1024);
	read(client_socket,str,sizeof(str));
	printf("%s\n",str);
	if(strcmp(str,"success") == 0){
		return 1;
	}
	return 0;
}

//删除离职人员、家属
int delete_0(int client_socket,int num){
	char username[20] = {0};
	printf("input 要删除的用户名:");
	scanf("%s",username);
	//创建json对象
	cJSON* root = cJSON_CreateObject();
	
	cJSON* type ;
	if(num == 1){
		type = cJSON_CreateString("delete_nurse");
	}else{
		type = cJSON_CreateString("delete_kin");
	}
	cJSON* username_0 = cJSON_CreateString(username);
	
	cJSON_AddItemToObject(root,"type",type);
	cJSON_AddItemToObject(root,"username",username_0);
	
	char str[1024] = {0};
	strcpy(str,cJSON_Print(root));
	printf("%s\n",str);
	write(client_socket,str,strlen(str));
	memset(str,0,1024);
	read(client_socket,str,sizeof(str));
	printf("%s\n",str);
	if(strcmp(str,"success") == 0){
		return 1;
	}
	return 0;
}

//员工、家属修改密码
int change_passwd(int client_socket,int num){
	char username[20] = {0};
	char passwd[20] = {0};
	char newpasswd[20] = {0};
	printf("input 用户名:");
	scanf("%s",username);
	printf("input 密码:");
	scanf("%s",passwd);
	printf("input 新密码:");
	scanf("%s",newpasswd);
	
	//创建json对象
	cJSON* root = cJSON_CreateObject();
	cJSON* type;
	if(num == 1){
		type = cJSON_CreateString("staff_change_passwd");//员工修改密码
	}else{
		type = cJSON_CreateString("kin_change_passwd");//家属修改密码
	}
	cJSON* username_0 = cJSON_CreateString(username);
	cJSON* passwd_0 = cJSON_CreateString(passwd);
	cJSON* newpasswd_0 = cJSON_CreateString(newpasswd);
	
	cJSON_AddItemToObject(root,"type",type);
	cJSON_AddItemToObject(root,"username",username_0);
	cJSON_AddItemToObject(root,"passwd",passwd_0);
	cJSON_AddItemToObject(root,"newpasswd",newpasswd_0);
	
	char str[1024] = {0};
	strcpy(str,cJSON_Print(root));
	printf("%s\n",str);
	write(client_socket,str,strlen(str));
	memset(str,0,1024);
	read(client_socket,str,sizeof(str));
	printf("%s\n",str);
	if(strcmp(str,"success") == 0){
		return 1;
	}
	return 0;
}

//员工请假
int leave(int client_socket){
	char username[20] = {0};
	char reason[100] = {0};
	char start_time[20] = {0};
	char end_time[20] = {0};
	printf("input 姓名:");
	scanf("%s",username);
	printf("input 请假理由:");
	scanf("%s",reason);
	printf("input 开始时间:");
	scanf("%s",start_time);
	printf("input 结束时间:");
	scanf("%s",end_time);
	
	//创建json对象
	cJSON* root = cJSON_CreateObject();
	
	cJSON* type = cJSON_CreateString("staff_leave");
	cJSON* username_0 = cJSON_CreateString(username);
	cJSON* reason_0 = cJSON_CreateString(reason);
	cJSON* start_time_0 = cJSON_CreateString(start_time);
	cJSON* end_time_0 = cJSON_CreateString(end_time);
	
	cJSON_AddItemToObject(root,"type",type);
	cJSON_AddItemToObject(root,"username",username_0);
	cJSON_AddItemToObject(root,"reason",reason_0);
	cJSON_AddItemToObject(root,"start_time",start_time_0);
	cJSON_AddItemToObject(root,"end_time",end_time_0);
	
	char str[1024] = {0};
	strcpy(str,cJSON_Print(root));
	printf("%s\n",str);
	write(client_socket,str,strlen(str));
	memset(str,0,1024);
	read(client_socket,str,sizeof(str));
	printf("%s\n",str);
	if(strcmp(str,"success") == 0){
		return 1;
	}
	return 0;
}

















